//
//  LSRwebViewController.m
//  BaiSiProject
//
//  Created by lshenrong on 16/7/10.
//  Copyright © 2016年 lshenrong. All rights reserved.
//

#import "LSRwebViewController.h"
#import <WebKit/WebKit.h>

@interface LSRwebViewController ()
@property (weak, nonatomic) IBOutlet UIView *containerView;

@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *fowardItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshItem;


@property(weak,nonatomic)WKWebView *webView;

@end

@implementation LSRwebViewController
- (IBAction)back:(id)sender {
    
    [self.webView goBack];
    
}
- (IBAction)foward:(id)sender {
    
     [self.webView goForward];
    
}
- (IBAction)refresh:(id)sender {
    
    [self.webView reload];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建webView,设置frame等于self.view.bounds
  WKWebView *webView=[[WKWebView alloc ] initWithFrame:self.view.bounds];
    webView.backgroundColor=[UIColor redColor];
    self.webView=webView;
    //把containerView的颜色设置透明
    
    [self.containerView addSubview:webView];
    //展示网页
 NSURLRequest *request=[NSURLRequest requestWithURL:self.url];
    [webView loadRequest:request];
    
    [self.webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    
    [self.webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    
    
    
}


-(void)dealloc
{

    [_webView  removeObserver:self   forKeyPath:@"canGoBack"];
    
    [_webView  removeObserver:self  forKeyPath:@"canGoForward"];

    [_webView removeObserver:self  forKeyPath:@"estimatedProgress"];


}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    NSLog(@"%@",change[@"NEW"]);

    
    self.progressBar.progress=self.webView.estimatedProgress;
    
    self.backItem.enabled=self.webView.canGoBack;
    
    self.fowardItem.enabled=self.webView.canGoForward;

}


@end
